#!/usr/bin/env bash
ME="[tovid-test]:"
. tovid-init

# tovid-test
# Part of the tovid suite
# =======================
# A bash script for testing the tovid suite.
#
# Project homepage: http://tovid.wikia.com
#
# Copyright (C) 2005-2010
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Or see:
#
#     http://www.gnu.org/licenses/gpl.txt

# ******************************************************************************
# ******************************************************************************
#
#
# DEFAULTS AND GLOBALS
#
#
# ******************************************************************************
# ******************************************************************************

SEPARATOR="========================================================="

SCRIPT_NAME=`cat << EOF
--------------------------------
tovid-test
Part of the tovid suite, version $TOVID_VERSION
$TOVID_HOME_PAGE
--------------------------------
EOF`

USAGE=`cat << EOF
Usage: tovid-test [OPTIONS] -in FILE

OPTIONS may be any of the following:

    -basic   Test VCD, SVCD, and DVD (Default)
    -dvd     Test DVD-compatible formats
    -all     Test all formats that tovid supports
    -ffmpeg  Test the ffmpeg backend
    -keep    Keep all output files

The input file may be any video file. Output
and logging go into a temporary directory
with a name like "tovid-test.1".
EOF`

KEEP_OUTPUT=false
DEBUG=false
ERRORS=0
LOG_FILE="tovid-test.log"
DEFAULT_OUTFILE="outfile.default"
TEST_FORMATS="vcd svcd dvd"
TEST_FFMPEG=false

get_args()
{
    while test $# -gt 0; do
        case "$1" in
            "-in" )
                # Input file to test
                shift
                INFILE="$1"
                ;;
            "-debug" )
                # Run tovid in -debug mode and print all output
                DEBUG=:
                ;;
            "-keep" )
                # Keep all output files, giving them unique names
                KEEP_OUTPUT=:
                ;;
            "-all" )
                # Test all formats
                TEST_FORMATS="dvd half-dvd svcd dvd-vcd vcd kvcd kdvd bdvd"
                ;;
            "-basic" )
                # Test three basic formats
                TEST_FORMATS="vcd svcd dvd"
                ;;
            "-dvd" )
                # Test the DVD-compatible formats
                TEST_FORMATS="dvd half-dvd dvd-vcd kdvd bdvd"
                ;;
            "-ffmpeg" )
                # Test the ffmpeg backend
                TEST_FFMPEG=:
                ;;
            "-cd" )
                # Test the CD-compatible formats
                TEST_FORMATS="vcd svcd"
                ;;
            * )
                usage_error "Unrecognized command-line option: '$1'"
                ;;
        esac
        shift
    done
}

# ******************************************************************************
# Y-echo: echo to two places at once (stdout and logfile)
# Output is preceded by the script name that produced it
# Args: $@ == any text string
# If no args are given, echo a separator bar
# Why echo when you can yecho?
# ******************************************************************************
yecho()
{
    if test $# -eq 0; then
        printf "\n%s\n\n" "$SEPARATOR"
        # If logfile exists, copy output to it (with pretty formatting)
        test -e "$LOG_FILE" && \
            printf "%s\n%s %s\n%s" "$ME" "$ME" "$SEPARATOR" "$ME" >> "$LOG_FILE"
    else
        echo "$@"
        test -e "$LOG_FILE" && \
            printf "%s %s" "$ME" "$@" >> "$LOG_FILE"
    fi
}

# ******************************************************************************
# Print usage notes and optional error message, then exit.
# Args: $@ == text string containing error message
# ******************************************************************************
usage_error()
{
    echo $"$USAGE"
    echo "$SEPARATOR"
    echo $@
    exit 1
}

# ******************************************************************************
# Test the tovid script with the given options and infile
# Args: $@ = all options (including -in) to test
#   -out filename will be ignored if provided
# ******************************************************************************
test_tovid()
{
    # If we're keeping every output file, generate a unique name
    if $KEEP_OUTPUT; then
        OUTFILE_UNIQUE=`echo $@ | sed 's/ //g'`
        OUTFILE="$OUTFILE_UNIQUE"
    else
        OUTFILE="$DEFAULT_OUTFILE"
        rm -f "$OUTFILE"
    fi
    CMD="tovid $@ -overwrite -out $OUTFILE"
    $TEST_FFMPEG && CMD="$CMD -ffmpeg"
    yecho $CMD
    if $DEBUG; then
        eval "$CMD" | tee -a "$LOG_FILE"
    else
        eval "$CMD" >> "$LOG_FILE"
    fi

    if test -e "$OUTFILE.mpg"; then
        # Report success
        echo "--------"
        echo "Success!"
        echo "$ME Detected a successful encoding" >> $LOG_FILE
        echo $SEPARATOR
        # TODO: Verify that output specs match the target format
        idvid "$OUTFILE" >> "$LOG_FILE"
    else
        # Count and log error
        ERRORS=`expr $ERRORS \+ 1`
        echo "$ME Detected an encoding failure" >> $LOG_FILE
        # Report failure
        echo "----------------"
        echo "*** Failure! ***"
        echo "Please see $LOG_FILE for error messages."
        # Report error
        echo $SEPARATOR
    fi
}

# ******************************************************************************
# Execution begins here
# ******************************************************************************
INFILE=""
get_args "$@"
if test -z "$INFILE"; then
    usage_error "Please provide the name of a video file to use as input."
fi

# Remove any existing log file
test -f "$LOG_FILE" && rm -fv "$LOG_FILE"

# Test each format in the list
for FMT in `echo $TEST_FORMATS`
do
    echo "Encoding to $FMT format"
    test_tovid "-$FMT -in $INFILE"
done

echo "There were $ERRORS errors. Please see $LOG_FILE for a complete report."
echo "Done. Thanks for using tovid-test."

exit 0
